package com.ll.menu.user.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.droid.library.app.adapter.BaseAbstractAdapter;
import com.droid.library.dialog.AbsDialog;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Table;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 切换桌号Dialog
 *
 * @author wangheng
 */

public class SwitchTableDialog extends AbsDialog {

    @InjectView(R.id.tvSwitchTableDialogCommit)
    TextView mButton;

    @InjectView(R.id.spSwitchTableDialogEditText)
    AppCompatSpinner mSpinner;

    public SwitchTableDialog(@NonNull Context context) {
        super(context);
    }

    protected SwitchTableDialog(@NonNull Context context,
                                boolean cancelable,
                                @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_switch_table);

        ButterKnife.inject(this);

        initSpinner();

    }

    private static final int MAX_COUNT = 30;
    private ArrayList<Table> mList = new ArrayList<>(MAX_COUNT);

    private void initSpinner() {
        for(int i = 0;i < 10;i++){
            Table table = new Table();
            table.setType(1);
            table.setId(String.valueOf(i + 1 + 20));
            table.setName((i + 1) + "号包间");
            mList.add(table);
        }

        for(int i = 0;i < 20;i++){
            Table table = new Table();
            table.setType(2);
            table.setId(String.valueOf(i + 1));
            table.setName((i + 1) + "号桌");
            mList.add(table);
        }

        final Adapter adapter = new Adapter(getContext());
        mSpinner.setAdapter(adapter);
        adapter.resetDataList(mList);
        mSpinner.setAdapter(adapter);

        mCurrentTable = mList.get(0);
        mSpinner.setSelection(0,false);

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mCurrentTable = adapter.getItem(position);
                mSpinner.setSelection(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                mCurrentTable = adapter.getItem(0);
                mSpinner.setSelection(0);
            }
        });
    }

    @OnClick({R.id.tvSwitchTableDialogCommit})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tvSwitchTableDialogCommit:
                dismiss();
                break;
            default:

                break;
        }
    }

    private Table mCurrentTable;

    public Table getCurrentTable(){
        return mCurrentTable;
    }

    private class Adapter extends BaseAbstractAdapter<Table> {

        Adapter(Context context){
            super(context);
        }
        @Override
        protected int getLayoutId() {
            return R.layout.item_switch_table;
        }

        @Override
        protected void inflate(View convertView, Table table, final int position) {
            TextView textView = (TextView) findViewById(R.id.tvDialogSwitchTableItem);
            textView.setText(table.getName());
        }
    }
}
